package com.gutsyzhan.yusiailangchain4j.controller;

import com.gutsyzhan.yusiailangchain4j.assistant.Assistant;
import com.gutsyzhan.yusiailangchain4j.assistant.MemoryChatAssistant;
import com.gutsyzhan.yusiailangchain4j.assistant.SeparateChatAssistant;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.service.AiServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private OllamaChatModel ollamaChatModel;

    @Autowired
    private Assistant assistant;

    @Autowired
    private MemoryChatAssistant memoryChatAssistant;

    @Autowired
    private SeparateChatAssistant separateChatAssistant;


//    @GetMapping("/testChat")
//    public String testChat(){
//        //创建AIService
//        Assistant assistant = AiServices.create(Assistant.class, ollamaChatModel);
//        //调用service的接口
//        return assistant.chat("你好,你是谁？");
//    }

    @GetMapping("/testChat")
    public String testChat(){
        //调用service的接口
        return assistant.chat("你好,你是谁？");
    }

    @GetMapping("/testChatMemory")
    public String testChatMemory(){
        String answer = assistant.chat("我是张三！");
        log.info(answer);
        return assistant.chat("我是谁？");
    }

    /**
     * 使用ChatMemory实现聊天记忆
     */
    @GetMapping("/testChatMemoryV2")
    public String testChatMemoryV2(){
        //创建Memory
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        //创建AIService
        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(ollamaChatModel)
                .chatMemory(chatMemory)
                .build();
        //调用service的接口
        String answer = assistant.chat("我是张三");
        log.info(answer);
        return assistant.chat("我是谁");
    }

    /**
     * 使用AIService实现聊天记忆
     */
    @GetMapping("/testChatMemoryV3")
    public String testChatMemoryV3(){
        String answer1 = memoryChatAssistant.chat("我是张三");
        log.info(answer1);
        String answer2 = memoryChatAssistant.chat("我是谁");
        log.info(answer2);
        return answer2;
    }

    /**
     * 隔离聊天记忆
     */
    @GetMapping("/testChatMemoryV4")
    public String testChatMemoryV4(){
        String answer1 = separateChatAssistant.chat(1,"我是张三");
        log.info("answer1:{}",answer1);
        String answer2 = separateChatAssistant.chat(1,"我是谁");
        log.info("answer2:{}",answer2);
        String answer3 = separateChatAssistant.chat(2,"我是谁");
        log.info("answer3:{}",answer3);
        return answer3;
    }
}
